#include <iostream>
#include <cstring>
#include <algorithm>
#include <sstream>
using namespace std;

int reverse1(int x) {
    cout<<x<<endl;
    int t=0;
    while(x!=0){
        t=x%10;
        x/=10;

    }
    cout<<t<<endl;
    string y = to_string(x);
    reverse(y.begin(),y.end());
    cout<<y<<endl;
    stringstream stream;
    stream<<y;
    int result;
    stream>>result;
    return result;
}
int reverse(int x) {
    cout<<x<<endl;
    int t=0,result=0;
    while(x!=0){
        t=x%10;
        x/=10;
        cout<<x<<t<<endl;
        if((result>INT_MAX/10)||(result==INT_MAX/10&&t>7))return 0;
        if(result<INT_MIN/10||(result==INT_MIN/10&&t<-8))return 0;
        result=result*10+t;
    }
    return result;
}
int main() {
    cout<<"reverse:"<<reverse(-123)<<endl;
    cout<<INT_MAX<<"\n"<<INT_MIN<<endl;
    return 0;
}